﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace JinYuan.ControlLib
{
    public partial class ArrTotalQty : UserControl
    {
        public ArrTotalQty()
        {
            InitializeComponent();

            //设置控件样式，去除缓冲
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // UpData();
        }


        //#region 减少闪烁
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}
        //#endregion

        private int _MaxValue = 999999;


        private int totalValue = 0;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示数字值:(最大值999999)")]


        public int TotalValue
        {
            get { return totalValue; }
            set
            {
                if (value > _MaxValue) return;
                totalValue = value;
                SetElectricEnergy(totalValue);
            }
        }


        private void SetElectricEnergy(int value)
        {
            if (totalValue.ToString().Length < 7)
            {
                bool isNumeric = Regex.IsMatch(totalValue.ToString(), @"^\d+$");
                if (isNumeric)
                {
                    List<int> numbers = new List<int>();

                    foreach (char c in totalValue.ToString())
                    {
                        if (char.IsDigit(c))
                        {
                            int number = int.Parse(c.ToString());
                            numbers.Add(number);
                        }
                    }

                    for (int i = 1; i < numbers.Count + 1; i++)
                    {
                        switch (numbers.Count)
                        {
                            case 1:
                                this.lbl_Title1.TitleName = 0.ToString();
                                this.lbl_Title2.TitleName = 0.ToString();
                                this.lbl_Title3.TitleName = 0.ToString();
                                this.lbl_Title4.TitleName = 0.ToString();
                                this.lbl_Title5.TitleName = 0.ToString();
                                this.lbl_Title6.TitleName = numbers[0].ToString();
                                break;
                            case 2:
                                this.lbl_Title1.TitleName = 0.ToString();
                                this.lbl_Title2.TitleName = 0.ToString();
                                this.lbl_Title3.TitleName = 0.ToString();
                                this.lbl_Title4.TitleName = 0.ToString();
                                this.lbl_Title5.TitleName = numbers[0].ToString();
                                this.lbl_Title6.TitleName = numbers[1].ToString();
                                break;
                            case 3:
                                this.lbl_Title1.TitleName = 0.ToString();
                                this.lbl_Title2.TitleName = 0.ToString();
                                this.lbl_Title3.TitleName = 0.ToString();
                                this.lbl_Title4.TitleName = numbers[0].ToString();
                                this.lbl_Title5.TitleName = numbers[1].ToString();
                                this.lbl_Title6.TitleName = numbers[2].ToString();
                                break;
                            case 4:
                                this.lbl_Title1.TitleName = 0.ToString();
                                this.lbl_Title2.TitleName = 0.ToString();
                                this.lbl_Title3.TitleName = numbers[0].ToString();
                                this.lbl_Title4.TitleName = numbers[1].ToString();
                                this.lbl_Title5.TitleName = numbers[2].ToString();
                                this.lbl_Title6.TitleName = numbers[3].ToString();
                                break;
                            case 5:
                                this.lbl_Title1.TitleName = 0.ToString();
                                this.lbl_Title2.TitleName = numbers[0].ToString();
                                this.lbl_Title3.TitleName = numbers[1].ToString();
                                this.lbl_Title4.TitleName = numbers[2].ToString();
                                this.lbl_Title5.TitleName = numbers[3].ToString();
                                this.lbl_Title6.TitleName = numbers[4].ToString();
                                break;
                            case 6:
                                this.lbl_Title1.TitleName = numbers[0].ToString();
                                this.lbl_Title2.TitleName = numbers[1].ToString();
                                this.lbl_Title3.TitleName = numbers[2].ToString();
                                this.lbl_Title4.TitleName = numbers[3].ToString();
                                this.lbl_Title5.TitleName = numbers[4].ToString();
                                this.lbl_Title6.TitleName = numbers[5].ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
}
